package com.villaday.idf.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.activity.AddrManagerActivity;
import com.villaday.idf.activity.AdviceActivity;
import com.villaday.idf.activity.BindPhoneActivity;
import com.villaday.idf.activity.CommonActivity;
import com.villaday.idf.activity.LoginActivity;
import com.villaday.idf.activity.MyCollectActivity;
import com.villaday.idf.activity.MyFocusActivity;
import com.villaday.idf.activity.MyIntegralActivity;
import com.villaday.idf.activity.MyOrderActivity;
import com.villaday.idf.activity.SettingActivity;
import com.villaday.idf.base.BaseFragment;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.StatusBarUtil;
import com.villaday.idf.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 我的
 */
public class MineFragment extends BaseFragment implements OkHttpUtil.OnDataListener {


    @BindView(R.id.sdv_head)
    SimpleDraweeView sdvHead;
    @BindView(R.id.tv_name)
    TextView tvName;

    @BindView(R.id.tv_collect)
    TextView tvCollect;

    @BindView(R.id.view_collect)
    View viewCollect;


    @BindView(R.id.tv_phone)
    TextView tvPhone;


    Unbinder unbinder;
    private Gson gson = new GsonBuilder().create();

    private boolean isLogin = true;//是否登录
    private RefreshtokenEntity refreshtoken;
    private MessageFragment.Jump2Page jump2Page;

    public MineFragment() {
        // Required empty public constructor
    }

    public static MineFragment instance() {
        MineFragment mineFragment = new MineFragment();
        return mineFragment;
    }

    @Override
    protected void init(View v) {
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), null);
        StatusBarUtil.setLightMode(getActivity());
        RefreshtokenEntity.BodyBean roleInfo =
                SharedPreferencesUtil.getRoleInfo(getContext());
        String roleType = roleInfo.getMember().getType();
        tvCollect.setVisibility("0".equals(roleType) ? View.GONE : View.VISIBLE);
        viewCollect.setVisibility("0".equals(roleType) ? View.GONE : View.VISIBLE);
    }

    @Override
    protected int getViewResId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void loadData() {
        Map<String, Object> map = new HashMap<>();
        OkHttpUtil.postJson(getContext(), Constant.URL.refresh_token, null, map, this);
    }

    @OnClick({ R.id.tv_advice,R.id.tv_addrManager, R.id.sdv_head, R.id.tv_name, R.id.tv_order, R.id.tv_coupons, R.id.tv_collect, R.id.tv_focus, R.id.tv_integral, R.id.tv_phone, R.id.tv_setting})
    public void onViewClicked(View view) {

        if (isLogin) {
            switch (view.getId()) {
                case R.id.tv_advice:
                {
                    Intent intent = new Intent(getActivity(), AdviceActivity.class);
                    startActivity(intent);
                }

                    break;

                case R.id.sdv_head://头像
                    break;
                case R.id.tv_name://昵称
                    break;
                case R.id.tv_order://我的订单
                {
                    Intent intent = new Intent(getContext(), MyOrderActivity.class);
                    startActivityForResult(intent, Constant.Code.OrderCode);
                }


                break;
                case R.id.tv_coupons://我的优惠券
                    break;
                case R.id.tv_collect://我的收藏
                {

                    Intent intent = new Intent(getContext(), MyCollectActivity.class);
                    startActivityForResult(intent, 123);
                }
                break;
                case R.id.tv_focus://我的喜欢
                {
                    Intent intent = new Intent(getContext(), MyFocusActivity.class);

                    startActivityForResult(intent, 124);

                }
                break;
                case R.id.tv_integral://我的积分
                {
                    Intent intent = new Intent(getContext(), MyIntegralActivity.class);

                    startActivity(intent);

                }
                break;
                case R.id.tv_phone://绑定/修改手机号
                {
                    Intent intent = new Intent(getContext(), BindPhoneActivity.class);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }

                break;
                case R.id.tv_setting://设置
                {
                    Intent intent = new Intent(getContext(), SettingActivity.class);

                    startActivity(intent);
                }
                break;
                case R.id.tv_addrManager: {//地址管理
                    startActivity(new Intent(getContext(), CommonActivity.class));
                }
                break;
            }
        } else {
            Intent intent = new Intent(getContext(), LoginActivity.class);

            startActivity(intent);
        }

    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.refresh_token.equals(url)) { //刷新Token
                LogeUtil.e("refresh_token", json);
                RefreshtokenEntity refreshtoken = gson.fromJson(json, RefreshtokenEntity.class);
                if (Constant.Strings.SUC.equals(refreshtoken.getCode())) {
                    //改变 主页bottom vip 图标及文字颜色
                    mRxManager.post(getResources().getString(R.string.ChangeMainBotton_vip_dirwableAndColor),refreshtoken.getBody().getMember().getType());

                    SharedPreferencesUtil.saveRoleInfo
                            (getActivity(), json);

                    SharedPreferencesUtil.saveToken(getContext(), refreshtoken.getBody().getToken());
                    UpdateUI(refreshtoken);
                } else {
                    ToastUtil.initToast(getContext(), refreshtoken.getMsg());
                }
            }
        }
    }

    private void UpdateUI(RefreshtokenEntity refreshtoken) {
        sdvHead.setImageURI(refreshtoken.getBody().getMember().getHeaderUrl());
        tvName.setText(refreshtoken.getBody().getMember().getNickName());
        if (TextUtils.isEmpty(refreshtoken.getBody().getMember().getMobile())) {
            tvPhone.setText("绑定手机号");
        } else {
            tvPhone.setText("修改手机号");
        }

    }

    @Override
    public void onFailure(String url, String error) {

    }


    public OnControlPageListener CPListener;

    public interface OnControlPageListener {
        public void main();
    }

    public void setOnControlPageListener(OnControlPageListener listener) {
        this.CPListener = listener;
    }

    public void setjump2Page(MessageFragment.Jump2Page jump2Page) {

        this.jump2Page = jump2Page;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constant.Code.OrderCode) {
            if (resultCode == Activity.RESULT_OK) {
                if (CPListener != null) {
                    CPListener.main();
                }
            }
        }
        if (requestCode == 123) {//我的收藏

            if (resultCode == Activity.RESULT_OK) {
                if (jump2Page != null) {
                    jump2Page.jump2Page(0, 0);
                }
            }

        }
        if (requestCode == 124) {//我的喜欢

            if (resultCode == Activity.RESULT_OK) {


                if (jump2Page != null) {
                    RefreshtokenEntity.BodyBean roleInfo =
                            SharedPreferencesUtil.getRoleInfo(getContext());
                    String roleType = roleInfo.getMember().getType();
                    if ("0".equals(roleType)) {
                        jump2Page.jump2Page(0, 0);

                    } else {
                        jump2Page.jump2Page(0, 1);

                    }
                }
            }

        }
    }
}
